import key from 'keymaster'
export default {
	namespace:'count',
	subscriptions:{
		keyboardWatcher({dispatch}){
			key('ctrl+up',()=>{dispatch({type:'add'})})
		}
	},
	state:{
		record:0,
		current:0
	},
	reducers:{
		add:(state)=>{
			const newCurrent=state.current+1;
			return {
				...state,
				record:newCurrent>state.record?newCurrent:state.record,
				current:newCurrent
			}
		},
		minus:(state)=>{
			return {
				...state,current:state.current-1
			}
		}
	},
	effects:{
		add:function* (action,{call,put}){
			yield call(delay,1000);
			yield put({type:'minus'})
		}//*add(){}
	}
}
function delay(timeout){
	return new Promise(resolve=>{
		setTimeout(resolve,timeout)
	})
}